package com.bobo.controller.back.user;

import com.alibaba.druid.util.StringUtils;
import com.bobo.annotations.RequestBody;
import com.bobo.annotations.RequestMapping;
import com.bobo.controller.base.BaseServlet;
import com.bobo.dto.Page;
import com.bobo.model.User;
import com.bobo.service.user.IUserService;
import com.bobo.service.user.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by bobo on 16-10-28.
 */
@WebServlet(name = "User",urlPatterns = {"/admin/user/*"})
public class UserBack extends BaseServlet {

    IUserService userService = new UserServiceImpl();

    @RequestMapping("/getUsers")
    public String getUsers(HttpServletRequest request) {
        int current = 1;
        int pageSize = 5;
        int status = 1;

        if (request.getParameter("current") != null && !StringUtils.equals(request.getParameter("current"), "")) {
            current = Integer.valueOf(request.getParameter("current"));
        }
        if (request.getParameter("pageSize") != null && !"".equals(request.getParameter("pageSize"))) {
            pageSize = Integer.valueOf(request.getParameter("pageSize"));
        }
        if (request.getParameter("status") != null && !"".equals(request.getParameter("status"))){
            status = Integer.valueOf(request.getParameter("status"));
        }
        Page<User> page = userService.getUsers(current, pageSize, status);
        request.setAttribute("page",page);
        return "jsp/back/user/getUsers";

    }

    @RequestBody
    @RequestMapping("/changeStatus")
    public String changeStatus(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        User user = userService.getUser(id);
        int status = Integer.parseInt(request.getParameter("status"));
        user.setStatus(status);
        userService.upd(user);
        return "true";
    }

    @RequestMapping("/add")
    public String add(User user) {
        userService.create(user);
        return "index.jsp";
    }



}
